package cn.zqw.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class MybatisUtil {
    private static SqlSessionFactory sqlSessionFactory;
static {

    try{
        //使用mybatis：从配置文件xml获取SqlSessionFactory对象
            String resource = "mybatis-config.xml";
            //加载 xml文件 生成输入流对象
            InputStream inputStream = Resources.getResourceAsStream(resource);
            //SqlSessionFactory 对象读取 xml输入流
             sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    //获取数据库连接资源
    //SqlSession完全包含了面向数据库执行sql命令所需的所有方法
    public static SqlSession getDatasource(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }


}
